﻿using System;
using Mss.Common.Helpers;
using Mss.Data;

namespace Mss.Managers
{
    public sealed class SecurityManager:NotifyPropertyChangedBase
    {
        #region Events

        public event EventHandler CurrentUserChanged;

        #endregion

        #region Fields
        
        private User _user; 
        
        #endregion

        #region Singleton Constructor

        private static SecurityManager _instance;

        private SecurityManager()
        {
        }

        public static SecurityManager Current
        {
            get { return _instance ?? (_instance = new SecurityManager()); }
        }

        #endregion

        #region Properties
        
        public User User
        {
            get { return _user; }
            private set
            {
                if (_user == value) return;
                
                _user = value;

                //Notify that current user is changed.
                if (CurrentUserChanged != null)
                {
                    CurrentUserChanged.Invoke(this, EventArgs.Empty);
                }
                RaisePropertyChanged("User");
            }
        } 

        #endregion

        #region Methods

        public bool LogOn(string userName, string password)
        {
            User = RepositoriesManager.UsersRepository.LogOn(userName, password);
            return User != null;
        }

        #endregion
    }
}